var qcloud = require('./vendor/sdk/index');
var config = require('./config');

App({
  globalData: {
    userInfo: null,
    wxUserInfo: null,
    getUserInfoCallBackFun: null,
    navHeight: 0,
    application: config.application,
    imageUrl: config.locale ? config.service.host + '/profile' : config.service.host,
  },
  onLaunch: function () {
    console.log('app.js onLaunch', config);
    // 获取手机系统信息
    wx.getSystemInfo({
      success: res => {
        console.log('app.js onlaunch 获取手机系统信息', res);
        //导航高度 - 统一转换为rpx
        if (res.model === 'iPhone 5') {
          this.globalData.navHeight = (res.statusBarHeight + 46) * (100 / 42);
        } else {
          this.globalData.navHeight = (res.statusBarHeight + 46) * 2;
        }
        this.globalData.windowHeight = res.windowHeight * 2;
        this.globalData.windowWidth = res.windowWidth * 2;
        this.globalData.system = res.system;
        this.globalData.platform = res.platform;
        this.globalData.model = res.model;
      },
      fail(err) {
        console.log(err);
      }
    });
  },
  onShow: function () {
    console.log('app.js onShow');
  },
  onHide: function () {
    console.log('app.js onHide');
  },
  loginIteraction: function ({ code, nickName, avatarUrl }, callbackFun) {
    //登录互动-获取后台存储用户信息
    let self = this;
    qcloud.request({
      url: config.service.loginUrl,
      // 请求之前是否登陆，如果该项指定为 true，会在请求之前进行登录
      login: false,
      data: {
        code: code,
        name: nickName,
        photo: avatarUrl
      },
      success(res) {
        console.log('app.js > loginIteraction > success ', res);
        // 设置全局userInfo信息
        self.globalData.userInfo = {
          ...res.data.data
        };
        // 回调函数
        if (typeof callbackFun === 'function') {
          callbackFun(res.data.data);
        } else if (!callbackFun && self.globalData.getUserInfoCallBackFun && typeof self.globalData.getUserInfoCallBackFun === 'function') {
          // 处理第一次进入小程序因为弹出guide页导致获取用户信息中断的情况
          self.globalData.getUserInfoCallBackFun(res.data.data);
          self.globalData.getUserInfoCallBackFun = null;
        }
      }
    });
  },
  getUserInfo: function () {
    let self = this;
    return new Promise((resolve, reject) => {
      if (self.globalData.userInfo) {
        resolve(self.globalData.userInfo);
      } else {
        self.getWxUserInfo().then(resolve);
      }
    });
  },
  refreshUserInfo(cb) {
    this.getWxUserInfo(true).then(cb);
  },
  getWxUserInfo(isForce) {
    let self = this;
    return new Promise((resolve, reject) => {
      if (!isForce && self.globalData.wxUserInfo) {
        self.loginIteraction(self.globalData.wxUserInfo, resolve);
      } else {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.login({
          success: res => {
            wx.getUserInfo({
              success: function (successRes) {
                let wxUserInfo = {
                  code: res.code,
                  ...successRes.userInfo
                };
                self.globalData.wxUserInfo = wxUserInfo;
                self.loginIteraction(wxUserInfo, resolve);
              },
              fail: function (failRes) {
                // 获取用户信息失败后。请跳转授权页面
                console.log('app-fail', res);
                qcloud.request({
                  url: config.service.uuidUrl,
                  success: res => {
                    console.log('获取uuid =', res);
                    // 构造全局用户信息
                    self.globalData.userInfo = {
                      id: -1,
                      account: res.data.data,
                      role: -1,
                      name: '游客',
                      realName: '游客'
                    }
                    resolve(self.globalData.userInfo);
                  }
                });
              }
            });
          }
        });
      }
    });
  }
});
